using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace pop3
{
    public partial class searchForm : Form
    {
        string backupText;
        int c = 1;
        private bool isCaseSensitive;
        public searchForm()
        {
            InitializeComponent();
        }
        public searchForm(string text) {
            InitializeComponent();
            resultTB.Text = text;
            backupText = text;
            isCaseSensitive = caseCB.Checked;
        }

        private void searchB_Click(object sender, EventArgs e)
        {
            isCaseSensitive = caseCB.Checked;
            Regex.CacheSize = 0;
            MatchCollection mc;
            if (isCaseSensitive)
            {
                mc = Regex.Matches(resultTB.Text, "<*(\\S*\\d*\\a*" + searchTB.Text + "\\a*\\d*\\S*)");
            }
            else 
            {
                mc = Regex.Matches(resultTB.Text.ToLower(), "<*(\\S*\\d*\\a*" + searchTB.Text.ToLower() + "\\a*\\d*\\S*)");
                
            }
            //MessageBox.Show(mc.Count.ToString());
            if(mc.Count > 0)
            {
                matchL.Text = ("Matches: "+ mc.Count.ToString() +" ");
                
                foreach (Match i in mc)
                {
                    ListViewItem searchItem = this.searchView.Items.Add(c.ToString());
                    searchItem.SubItems.Add(i.ToString());
                    searchItem.SubItems.Add(i.Index.ToString());
                    searchItem.SubItems.Add(i.Length.ToString());

                    searchView.Update();
                    c++;
                }
            }


        }

        delegate void updateResultShowMatch();
        private void showMatch()
        {
            if (this.resultTB.InvokeRequired)
            {
                updateResultShowMatch u = new updateResultShowMatch(showMatch);
                this.Invoke(u, new object[] {});
            }
            else 
            {
                for (int i = 0; i < searchView.SelectedIndices.Count; i++)
                {
                    resultTB.SelectionBackColor = Color.Black;
                    resultTB.SelectionColor = Color.DarkOrange;
                    resultTB.Select(int.Parse(searchView.Items[searchView.SelectedIndices[i]].SubItems[2].Text), int.Parse(searchView.Items[searchView.SelectedIndices[i]].SubItems[3].Text));
                    resultTB.Update();
                } 

            }
        }



        private void dontShowB_Click(object sender, EventArgs e)
        {
            showMatch();
        }

        private void clearB_Click(object sender, EventArgs e)
        {
            resultTB.Clear();
            resultTB.Text = backupText;
            resultTB.Update();
            
        }

        private void clearListB_Click(object sender, EventArgs e)
        {
            searchView.Items.Clear();
            c = 1;
        }

    }
}